package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
import androidx.annotation.NonNull;
import android.text.TextUtils;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.websocket.AppDefine;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.DepthCoordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;

import java.util.HashMap;
import java.util.List;


class DepthLayer extends BasicLayer
{
	protected final double m_dbNone = Double.MIN_VALUE;
	private int m_color1 = AppDefine.ColorDef.UP_TRANS.getColor(),m_color2 = AppDefine.ColorDef.DOWN_TRANS.getColor(),m_color3 = 0xFF19C570,m_color4 = 0xFFED394D;

	public DepthLayer()
	{
		super();
	}

	@Override
	public Coordinate createCoordinate()
	{
		return new DepthCoordinate();
	}

	@Override
	public boolean onCalculateData()
	{
		return true;
	}


	protected int getDataStartIndex()
	{
		return 0;
	}


	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.Depth;
	}

	public List<CandleData> convertDate(List<CandleData> pList)
	{
		return pList;
	}

	@Override
	public void drawYesterClose(Canvas pCanvas, Paint pPaint)
	{
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		Coordinate tCoordinate = m_callback.getCoordinate();
		List<CandleData> tDataList = convertDate(m_callback.getCandleDataList());

		//定义初始化文本
		String tCloseText = null;
		String tOpenText = null;
		String tHighText = null;
		String tLowText = null;
		String tTimeText = null;
		String tValueText = null;
		float tVolumnText = 0;
		String tAvgText = null;
		int tXPoint = pPoint.x;
		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = 0; // tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue;
			float tYValueLength = tCoordinate.getYInterval() * tCoordinate.getYIntervalCount();
			RectF tChartRect = tCoordinate.getLayerRectF();

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;

			float tBidFirstPostion = 0.f;

			int tDotColor = R.color.sys_even;
			float tLineWidth = 2.f;
			Path tAreaPath = new Path();

			pPaint.setStrokeWidth(2);
			pPaint.setStyle(Paint.Style.FILL);
			pPaint.setAntiAlias(true);
			pPaint.setColor(m_color3);
			float tTopPosition = tCoordinate.getYFirstPosition();

			boolean bidComplete = false;
			for (int i = tStartIndex; i < tListSize; i++)
			{
				//如果收盘价为空就跳过
				if (tDataList.get(i).m_lVolume == m_dbNone)
				{
					continue;
				}

				float tVolumn = (float) tDataList.get(i).m_lVolume;

				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * i;
				tTempYPosition = tCurrentYPosition;
				tCurrentYPosition = tYFirstposition - ((tVolumn - tYMinValue) * tYValueLength / tYValueDist);

				tTopPosition = tTopPosition < tCurrentYPosition ? tTopPosition : tCurrentYPosition;


				if(tDataList.get(i).isFake()) {
					if (i == tStartIndex) {
						tAreaPath.moveTo(tCurrentXPosition, tChartRect.bottom);
					}

					tAreaPath.lineTo(tCurrentXPosition, tCurrentYPosition);
					if (i != tStartIndex)
					{
						pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
					}
				}
				else
				{
					if(!bidComplete) {

						pCanvas.drawLine(tTempXPosition, tTempYPosition, tTempXPosition, tChartRect.bottom, pPaint);
						tAreaPath.lineTo(tTempXPosition, tChartRect.bottom);
						tAreaPath.lineTo(tXFirstPosition, tChartRect.bottom);
//						Shader mShader = getLinearGradient(tChartRect, tTopPosition, m_color1);
//						pPaint.setShader(mShader);
						pPaint.setColor(m_color1);

						pCanvas.drawPath(tAreaPath, pPaint);
                        pPaint.reset();
						pPaint.setStrokeWidth(2);
						pPaint.setStyle(Paint.Style.FILL);
                        pPaint.setAntiAlias(true);
						pPaint.setColor(m_color4);
						bidComplete = true;
						tAreaPath = new Path();
						tAreaPath.moveTo(tCurrentXPosition, tChartRect.bottom);
						tBidFirstPostion = tCurrentXPosition;
						pCanvas.drawLine(tCurrentXPosition, tChartRect.bottom, tCurrentXPosition, tCurrentYPosition, pPaint);
					}
					else
					{
						pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
					}
					tAreaPath.lineTo(tCurrentXPosition, tCurrentYPosition);

					if (i == (tListSize - 1))
					{
						tAreaPath.lineTo(tCurrentXPosition, tChartRect.bottom);
						tAreaPath.lineTo(tBidFirstPostion, tChartRect.bottom);
//						Shader mShader = getLinearGradient(tChartRect, tTopPosition, m_color2);
//						pPaint.setShader(mShader);
						pPaint.setColor(m_color2);
						pCanvas.drawPath(tAreaPath, pPaint);
					}


				}

			}


			pPaint.reset();
			pPaint.setAntiAlias(true);
		}

		RectF tLayerRect = tCoordinate.getLayerRectF();

		setCrossData(pCanvas, pPaint, tTimeText, tValueText, new Point(tXPoint, pPoint.y));
		if (tLayerRect.contains(tXPoint, pPoint.y))
		{


			if (m_callback != null)
			{
				int tDigit = Math.max(tCoordinate.getDigit(), 1);

				tCloseText = getDigitString(tCloseText, tDigit);
				tOpenText = getDigitString(tOpenText, tDigit);
				tHighText = getDigitString(tHighText, tDigit);
				tLowText = getDigitString(tLowText, tDigit);
				tAvgText = getDigitString(tAvgText, tDigit);

				HashMap<String, String> tMap = new HashMap<String, String>();
				tMap.put("Close", tCloseText);
				tMap.put("Open", tOpenText);
				tMap.put("High", tHighText);
				tMap.put("Low", tLowText);
				tMap.put("Volumn", tVolumnText == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnText, 2));
				tMap.put("Avg",tAvgText);
				tMap.put("Time",tTimeText);
				m_callback.pointFeedback(tMap,pPoint );
			}
		}

	}

	@NonNull
	private LinearGradient getLinearGradient(RectF tChartRect, float tTopPosition, int color) {
		return new LinearGradient(0, tTopPosition, 0, tChartRect.bottom,
                Color.argb((Color.alpha(color)) / 3, Color.red(color), Color.green(color), Color.blue(color)), Color.argb((color) / 15, Color.red(color), Color.green(color), Color.blue(color)), Shader.TileMode.CLAMP);
	}


	private String getDigitString(String value, int digit)
	{
		if (! TextUtils.isEmpty(value))
		{
			try
			{
				value = FormatUtil.getFormatString(Double.parseDouble(value), digit);
			}
			catch (Exception e)
			{
			}
		}
		else
		{
			value = "";
		}

		return value;
	}

	private float getScale(float start, float end, float middle)
	{
		return (start - middle) / (start - end);
	}

	private void resetIndicatorData()
	{

	}

	/**
	 * draw layer left－top text
	 * 获取指标值进行显示
	 * @param pCanvas
	 * @param pPaint
	 */
	@Override
	public void drawFloatIndicatorText(boolean longtouch,Canvas pCanvas, Paint pPaint)
	{
	}
}
